/**
 * 基础工具类
 * @Author Lonely_man
 * @Date 2020/3/11 11:11
 * @Version 1.0
 */
layui.define(['jquery'], function (exports) {

    var $ = layui.jquery;

    var obj = {

        /**
         * 校验是否为空
         * @param obj
         * @returns {boolean}
         */
        isEmpty: function isEmpty(obj) {
            return typeof obj == 'undefined' || obj == null || obj === '';
        },

        sleep: function sleep(n){
            var start = new Date().getTime();
            while (true){
                if(new Date().getTime() - start > n){
                    break;
                }
            }
            return true;
        },

        /**
         * post参数转get
         * @param url
         * @param parms
         */
        postToGet: function(url,parms){
            var parmStr = "?";
            for(var key in parms){
                var parmTemp = key + "=" + parms[key] + "&";
                parmStr += parmTemp;
            }
            return url + parmStr.substr(0,parmStr.length-1);
        },

        /**
         * 异步提交
         * @param url 访问的Url
         * @param parms 参数{}
         */
        submit: function (url,parms) {
            var code = "";
            var msg = "";
            var data = null;
            $.ajax({
                url:url
                ,type:'post'
                ,data:parms
                ,async:false
                ,dataType:'json'
                ,success:function(res){
                    code = res.code;
                    msg = (typeof res.msg == 'undefined' || res.msg == null || res.msg === '')?"操作成功":res.msg;
                    if(typeof(res.data) != "undefined"){
                        data = res.data
                    }
                }
                ,error:function(){
                    msg = "网络故障";
                }
            });
            return {code:code,msg:msg,data:data};
        }

    };
    exports("tool",obj);
});